﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// List of check boxes one or more of which can be checked.
	/// </summary>
	public class CheckBoxList : BaseComplexMultiValueListControl, IComplexInputListControl
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public CheckBoxList()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#endregion
		#region PROPS
		#region DEF
		/// <summary>
		/// Props.
		/// </summary>
		protected static new Properties _clientProperties = Define(BaseComplexListControl._clientProperties, ComplexInputListControlCommon.Properties);
		/// <summary>
		/// Props.
		/// </summary>
		public override Properties ClientProperties
		{
			get
			{
				return _clientProperties;
			}
		}
		#endregion
		/// <summary>
		/// The direction in which items in this list will be rendered.
		/// </summary>
		public Orientation RepeatDirection
		{
			get
			{
				return this.GetProperty<Orientation>("RepeatDirection");
			}
			set
			{
				this.ViewState["RepeatDirection"] = value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region METHODS
		#region INIT
		/// <summary>
		/// Registers <see cref="Script.StringBuilder"/> and CheckBoxList.js.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);
			ComplexInputListControlCommon.RegisterIncludes(this, includes);

			includes.Add(Script.StringBuilder);
			includes.Add(Script.Array_Contains);
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.ListControl.CheckBoxList.js", typeof(CheckBoxList)));
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Renders an item group's begin tag.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		protected override void RenderItemGroupBeginTag(HtmlTextWriter writer, ListItem item)
		{
			ComplexInputListControlCommon.RenderItemGroupBeginTag(this, writer, item);
		}
		/// <summary>
		/// Renders a line-break if <see cref="CheckBoxList.RepeatDirection"/> is <see cref="Orientation.Vertical"/>.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		public override void RenderItemOption(HtmlTextWriter writer, ListItem item)
		{
			ComplexInputListControlCommon.RenderItemOption(this, writer, item);
		}
		/// <summary>
		/// Renders the check box for a list item.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		public void RenderItemOptionInternal(HtmlTextWriter writer, ListItem item)
		{
			if (item.Selected)
				writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
			writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
			writer.AddAttribute(HtmlTextWriterAttribute.Value, item.Value);
			writer.RenderBeginTag(HtmlTextWriterTag.Input);
			writer.RenderEndTag();
		}
		#endregion
		#endregion
		#endregion
	}
}